﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Configuration;

namespace WpfApp
{
    /// <summary>
    /// LoginWindow.xaml 的交互逻辑
    /// </summary>
    public partial class LoginWindow : Window
    {
        public LoginWindow()
        {
            InitializeComponent();
        }
        //实例化服务
        BLL.WDLogin.WDLoginClient myClient = new BLL.WDLogin.WDLoginClient();
        /// <summary>
        /// 页面加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            #region 全屏
            this.Top = 0;
            this.Left = 0;
            Rect rect = SystemParameters.WorkArea;
            this.Width = rect.Width;
            this.Height = rect.Height;
            #endregion
            //记住账号密码
            if (GetSettingString("isRemember") == "true")
            {
                loginCheckBoxUne.IsChecked = true;
                txtAccount.Text = GetSettingString("UserName");
                pwdPassword.Password = GetSettingString("UserPassword");
            }
            else
            {
                loginCheckBoxUne.IsChecked = false;
            }
            //自动登录
            if (GetSettingString("isLogin") == "true")
            {
                btnLogin_Click(null, null);
            }
            else
            {
                loginCheckBoxIs.IsChecked = false;
            }

        }
        /// <summary>
        /// 关闭应用程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("是否要退出系统？", "系统提示", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
            {
                //关闭应用程序
                Application.Current.Shutdown();
            }
        }
        /// <summary>
        /// 验证登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //获取页面数据
                string strAccount = txtAccount.Text.Trim();
                string strPassword = pwdPassword.Password.Trim();
                if (strAccount == string.Empty || strPassword == string.Empty)
                {
                    MessageBox.Show("请检查账号密码是否为空", "系统提示", MessageBoxButton.YesNo, MessageBoxImage.Error);
                }
                else
                {
                    DataTable dt = myClient.btnLogin_Click_CheckLogin(strAccount, strPassword).Tables[0];
                    if (dt.Rows.Count > 0)
                    {
                        if (Convert.ToBoolean(dt.Rows[0]["effective"]) == true)
                        {
                            //获取表格值
                            string strStaffName = dt.Rows[0]["staffName"].ToString().Trim();
                            string strBranch = dt.Rows[0]["branch"].ToString().Trim();
                            //打开窗口
                            MainWindow myMainWindow = new MainWindow(strStaffName, strBranch);
                            myMainWindow.Show();
                            //关闭当前窗口
                            this.Close();
                        }
                        else if (Convert.ToBoolean(dt.Rows[0]["effective"]) == false)
                        {
                            MessageBox.Show("该账号已被停用！", "提示！", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                    else
                    {
                        MessageBox.Show("你输入的账号密码有误", "系统提示", MessageBoxButton.YesNo, MessageBoxImage.Error);
                    }

                }

            }
            catch (Exception ex)
            {
                //设置页面透明
                dpLogin.Opacity = 0;
                string ex1 = string.Empty;
                // 获取描述当前异常的消息。
                if (ex.InnerException != null)
                {
                    ex1 = ex.InnerException.Message;
                }
                else
                {
                    ex1 = ex.Message;
                    ex1 = ex1.Split('。')[0];
                }
                if (ex1 == "无法连接到远程服务器")
                {
                    MessageBox.Show("服务器处于关闭状态，请启动！", "提 示", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                else if (ex1 == "在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误")
                {
                    MessageBox.Show("服务端SQL服务处于关闭状态！", "提 示", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                else
                {
                    MessageBox.Show("您断网了，或服务器处于关闭状态！", "提 示", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                Environment.Exit(0);  //中断程序
            }
            //记住用户名和密码
            if (Convert.ToBoolean(loginCheckBoxUne.IsChecked))
            {
                UpdateSettingString("UserName", txtAccount.Text);
                UpdateSettingString("UserPassword", pwdPassword.Password);
                UpdateSettingString("isRemember", "true");
                UpdateSettingString("isLogin", "");
                if (Convert.ToBoolean(loginCheckBoxIs.IsChecked))
                {
                    UpdateSettingString("isLogin", "true");
                }
            }
            else
            {
                UpdateSettingString("UserName", "");
                UpdateSettingString("UserPassword", "");
                UpdateSettingString("isRemember", "");
                UpdateSettingString("isLogin", "");
            }
        }
        /// <summary>
        /// 读取客户设置
        /// </summary>
        /// <param name="settingName"></param>
        /// <returns></returns>
        public static string GetSettingString(string settingName)
        {
            try
            {
                string settingString = ConfigurationManager.AppSettings[settingName].ToString();
                return settingString;
            }
            catch (Exception)
            {
                return null;
            };
        }
        /// <summary>
        /// 更新设置
        /// </summary>
        /// <param name="settingName"></param>
        /// <param name="valueName"></param>
        public static void UpdateSettingString(string settingName,string valueName)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            if (ConfigurationManager.AppSettings[settingName] != null)
            {
                config.AppSettings.Settings.Remove(settingName);
            }
            config.AppSettings.Settings.Add(settingName, valueName);
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }
    }
}
